/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import java.io.IOException;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import myconfession.dao.AccountDAO;
import myconfession.dto.account.Account;
import myconfession.dto.account.Accounts;
import myconfession.utils.ConstantProvider;
import myconfession.utils.XMLUtility;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 *
 * @author Liem
 */
public class AccountBO {

    public static void loadAndMarshall(String filePath) throws JAXBException {
        Accounts accounts = AccountDAO.getAllAccount();
        XMLUtility.marshallJAXB(filePath, accounts);
    }

    public static void setFbToken(String username, String fbToken)
            throws JAXBException {
        Accounts accounts = (Accounts) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getAccountXmlPath(), Accounts.class);
        for (Account account : accounts.getAccount()) {
            if (account.getUsername().equals(username)) {
                account.setFbToken(fbToken);
                break;
            }
        }
        XMLUtility.marshallJAXB(ConstantProvider.getInstance().getAccountXmlPath(), accounts);
    }

    public static void updateToXml(Account acc) throws JAXBException {
        if (AccountDAO.updateAccount(acc)) {
            Accounts accounts = (Accounts) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getAccountXmlPath(), Accounts.class);
            for (Account tmp : accounts.getAccount()) {
                if (tmp.getUsername().equals(acc.getUsername())) {
                    tmp.setFirstname(acc.getFirstname());
                    tmp.setLastname(acc.getLastname());
                    tmp.setEmail(acc.getEmail());
                    break;
                }
            }
            XMLUtility.marshallJAXB(ConstantProvider.getInstance().getAccountXmlPath(), accounts);
        }
    }
    public static boolean  createAccount(Account acc) throws JAXBException{
        if (AccountDAO.createAccount(acc)) {
            Accounts accounts = (Accounts) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getAccountXmlPath(), Accounts.class);
            accounts.getAccount().add(acc);
            XMLUtility.marshallJAXB(ConstantProvider.getInstance().getAccountXmlPath(), accounts);
            return true;
        }
        return false;
    }

    public static Account getAccountByUsername(String username) throws JAXBException {
        Accounts accounts = (Accounts) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getAccountXmlPath(), Accounts.class);
        for (Account tmp : accounts.getAccount()) {
            if (tmp.getUsername().equals(username)) {
                return tmp;
            }
        }
        return null;
    }
}
